﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Studio.Foundation.Ontologies;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Reflection;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (false == this.IsPostBack)
        {
            this.DropDownList1.DataSource = EntityCache.CACHED_ASSEMBLY.GetTypes();
            this.DropDownList1.DataTextField = "Name";
            this.DropDownList1.DataBind();
            this.DropDownList1.SelectedIndex = 0;

            this.EntityView1.Width = 500;
        }
    }

    string[] schemas = 
    {
        @"<?xml version=""1.0""?>
        <xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
        <xs:element name=""ButtonType"">
          <xs:complexType>
            <xs:sequence>
              <xs:element name=""名称"" type=""xs:string""/>
              <xs:element name=""文字"" type=""xs:string""/>
              <xs:element name=""宽度"" type=""xs:int""/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        </xs:schema>",

        @"<?xml version=""1.0""?>
        <xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">

        <xs:element name=""MenuType"">
          <xs:complexType>
            <xs:sequence>
              <xs:element name=""名称"" type=""xs:string""/>
              <xs:element name=""是否激活"" type=""xs:boolean""/>
              <xs:element name=""图标"" type=""xs:string""/>
              <xs:element name=""显示序号"" type=""xs:int""/>
              <xs:element name=""Url"" type=""xs:string""/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        </xs:schema>"
    };

    protected void Button2_Click(object sender, EventArgs e)
    {
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        foreach (string s in schemas)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);
            XmlReader reader = new XmlTextReader(new StringReader(doc.OuterXml));
            // Read the xsd
            XmlSchema xsd = schemaSet.Add(null, reader);
        }
        schemaSet.Compile();

        XsdCompiler compiler = new XsdCompiler(null, null, null);
        foreach (XmlSchema xsd in schemaSet.Schemas())
            compiler.AddSchema(xsd);

        Assembly assembly = compiler.Compile();
        Type[] types = assembly.GetTypes();
        if (types.Length < 1)
            throw new Exception();
        Type type = types[0];

        Console.WriteLine(type.FullName);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        this.TextBox1.Text = this.EntityView1.EntityString;
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Type type = EntityCache.FindTypeByTypeName(this.DropDownList1.SelectedItem.Text);
        this.EntityView1.EntityType = type;
        this.Button2.ValidationGroup = this.EntityView1.ValidationGroup;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        this.EntityView1.EntityString = this.TextBox1.Text;
    }
}